package io.dshubs.archives.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import io.dshubs.archives.dao.ArchiveWarehouseLocationMapper;
import io.dshubs.archives.dto.ArchiveNumberRuleDto;
import io.dshubs.archives.entity.ArchiveBaseRule;
import io.dshubs.archives.entity.ArchiveWarehouseLocation;
import io.dshubs.archives.entity.ArchiveWarehouseZone;
import io.dshubs.archives.service.ArchiveWarehouseLocationService;
import io.dshubs.archives.service.ArchiveWarehouseZoneService;
import io.dshubs.commons.log.annotation.LogOperation;
import io.dshubs.commons.tools.constant.Constant;
import io.dshubs.commons.tools.exception.RenException;
import io.dshubs.commons.tools.page.PageData;
import io.dshubs.commons.tools.utils.Result;
import io.dshubs.commons.tools.validator.ValidatorUtils;
import io.dshubs.commons.tools.validator.group.AddGroup;
import io.dshubs.commons.tools.validator.group.DefaultGroup;
import io.dshubs.commons.tools.validator.group.UpdateGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 库区表 前端控制器
 * </p>
 *
 * @author tan
 * @since 2021-08-16
 */
@Api(tags = "库区表")
@RestController
@RequestMapping("/warehouse-zone")
public class ArchiveWarehouseZoneController {

    @Autowired
    private ArchiveWarehouseZoneService archiveWarehouseZoneService;

    @Autowired
    private ArchiveWarehouseLocationMapper warehouseLocationMapper;

    @Autowired
    private ArchiveWarehouseLocationService archiveWarehouseLocationService;
    @GetMapping("page")
    @ApiOperation("库区:page")
    @LogOperation("库区:page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String") ,
            @ApiImplicitParam(name = "warehouseId", value = "库房id", paramType = "query", dataType="String"),
            @ApiImplicitParam(name = "zoneCode", value = "库区编码", paramType = "query", dataType="String"),
            @ApiImplicitParam(name = "zoneName", value = "库区名称", paramType = "query", dataType="String"),
    })
    public Result<PageData<ArchiveWarehouseZone>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        return new Result<PageData<ArchiveWarehouseZone>>().ok(archiveWarehouseZoneService.page(params));
    }


    @PostMapping
    @ApiOperation("保存")
    @LogOperation("库区:save")
    public Result save(@RequestBody ArchiveWarehouseZone archiveWarehouseZone){

        List<Long> locationIds = archiveWarehouseZone.getArchiveWarehouseLocationIds();
        if(CollectionUtils.isEmpty(locationIds)) {
            throw new RenException("货位不能为空");
        }
        archiveWarehouseZone.setId(IdWorker.getId());
        archiveWarehouseZoneService.insert(archiveWarehouseZone);
        List<ArchiveWarehouseLocation> locationList = warehouseLocationMapper.selectBatchIds(locationIds);
        locationList.forEach(i->{
            i.setZoneId(archiveWarehouseZone.getId());
        });
        archiveWarehouseLocationService.updateBatchById(locationList);
        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("库区:update")
    public Result update(@RequestBody ArchiveWarehouseZone archiveWarehouseZone){
        //效验数据
        ValidatorUtils.validateEntity(archiveWarehouseZone, UpdateGroup.class, DefaultGroup.class);
        archiveWarehouseZoneService.deleteById(archiveWarehouseZone);
        archiveWarehouseZoneService.save(archiveWarehouseZone);
        return new Result();
    }

    @DeleteMapping()
    @ApiOperation("删除")
    @LogOperation("库区:delete")
    public Result delete(@RequestBody @NotNull(message = "批量删除id不能为空") List<Long> ids){
        archiveWarehouseZoneService.deleteBatch(ids);
        return new Result();
    }
    @GetMapping("{id}")
    @ApiOperation("查看")
    public Result<ArchiveWarehouseZone> get(@PathVariable("id") String id){
        ArchiveWarehouseZone archiveWarehouseZone = archiveWarehouseZoneService.selectById(id);
        List<ArchiveWarehouseLocation> locationList = warehouseLocationMapper.selectList(new QueryWrapper<ArchiveWarehouseLocation>()
                .eq("zone_id", id)
                .eq("del_flag", 0)
        );
        archiveWarehouseZone.setArchiveWarehouseLocationList(locationList);
        return new Result<ArchiveWarehouseZone>().ok(archiveWarehouseZone);
    }



}
